<?php

namespace web\common\behavior;

class Init {

    public function run(&$request) {             
        $this->initHook();
    }

    private function initHook() {
        $data = cache('hooks');
        if (!$data) {
            $hookM = new \web\common\model\sys\HookModel();
            $addonsM = new \web\common\model\sys\AddonsModel();
            $hooks = $hookM->getHooksName();
            foreach ($hooks as $key => $value) {
                if ($value) {
                    $where = array('status' => 1);
                    $names = explode(',', $value);
                    $where['name'] = array('IN', $names);
                    $data = $addonsM->getAddonsName($where);
                    if ($data) {
                        $addons = array_intersect($names, $data);
                        \think\Hook::add($key, array_map('get_hook_class', $addons));
                    }
                }
            }
            cache('hooks', \think\Hook::get());
        } else {
            \think\Hook::import($data, false);
        }
    }

}